home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2147 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  152 lines

  1. Path: news.dx.net!NewsWatcher!user
  2. From: mikedorman@cedarnet.com (Mike Dorman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: Fri, 19 Jan 1996 07:25:37 -0600
  6. Organization: CedarNet Online
  7. Message-ID: <mikedorman-1901960725380001@205.148.200.150>
  8. References: <surgsw-1901960148530001@128.206.206.86>
  9. NNTP-Posting-Host: 205.148.200.150
  10.  
  11. In article <surgsw-1901960148530001@128.206.206.86>,
  12. surgsw@mizzou1.missouri.edu (Joel Weinstein) wrote:
  13.  
  14. > I have been trying to get this very simple piece of code to work for
  15. > hours.  What is the problem???????
  16. > #include <stdio.h>
  17. > main()
  18. > {
  19. >    int i=0;
  20. >    char  word[100], c;
  21.  
  22. word is an array of 100 chars, there are no pointers declared here.
  23. change word to this:
  24.       char *word;
  25. and it will be *much* easier.
  26.  
  27. >    printf("Enter a word:   ");
  28. >    while( (c = getchar()) != '\n')  {
  29. >       *word = c;
  30. here, replace *word (which is a pointer to something...you didn't specify
  31. which char in word you wanted it to point to, so it could be pointing to
  32. just about anything) with
  33.         strcat(word, *c);
  34. which will copy c onto the end of word, adding the character to word.
  35. >       word == word + 1;
  36. you don't need this...just get rid of it.
  37.  
  38. >    }  
  39. >    printf("You entered:  ");  
  40. >    *word = 0;
  41. >    while( *word != '\n' )   
  42. >    {  
  43. >       putchar( *word );
  44. >       word == word + 1; 
  45. >    }
  46. Just get rid of that whole while loop, and replace it with:
  47.      puts(word);
  48.  
  49. > }  
  50. > I think the problem I am having is due to my not knowing when to use the
  51. > '*' operator.  When do you use it?  Also, why won't my goddam compiler let
  52.  
  53. Ok, one problem at a time...
  54.  
  55. When you say "char word[100]", that defines a object (and object it
  56. anything in memory) called word, that contains 100 chars (or 100 byte
  57. values).  So, later when word is referenced (like any array) you need to
  58. tell the compiler which value in the array you want by adding brackets to
  59. the end (so, if you wanted the first char in word, you'd say word[0])
  60. (arrays are referenced from *ZERO* so array[0] is really the first value
  61. in the array).
  62.  
  63. When you use the * operator, that returns a pointer to something.  So
  64. *word returns a pointer to word, an array of chars.  I'm not sure how
  65. standard C deals with this, but i would recommend not doing it that way. 
  66. If you needed a pointer to a string from a char array (which is kindof
  67. what you need, sortof) you'd get one by getting a pointer to the first
  68. char (*word[0]).
  69.  
  70. Also, when getting string information from the user, you probably
  71. shouldn't use a char array (I know it seems to make more sense that way,
  72. but soon it'll become clear that it doesn't), but you should use a string
  73. pointer, like this:
  74.    char *word;
  75. Now, word is a pointer to a string, which has no specific or limited
  76. length.  Then you can use the standard functions like strcpy and strcat to
  77. work on string pointers.
  78.  
  79. To do it with an array, you need another variable to keep track of how
  80. many characters you've already put into word, so you can index the array
  81. correctly:
  82.  
  83.  
  84.  
  85. main()
  86. {
  87.    char word[100], c;
  88.    int i;
  89.    i = 0;
  90.  
  91.    puts("Enter a word:");
  92.    while((c = getch()) != \n)
  93.    {
  94.       word[i] = c;
  95.       i++;
  96.    }
  97.  
  98.    puts("You entered: ");
  99.    puts(*word[0]);
  100. }
  101.  
  102. But, if you did it with a string pointer, it's this much easier:
  103.  
  104. main()
  105.    char *word;
  106.  
  107.    puts("Enter a word: ");
  108.    while(scanf("%s", word) != 0)
  109.  
  110.    puts("You entered: ");
  111.    puts(word);
  112. }
  113.  
  114.   
  115. > me do word++; instead of word == word +1;   I also tried to do *word++;
  116. > and it didn't work, what is the deal with that.  It won't let me put word
  117. > = word + 1;.  It insists on the double ='s.  Why is that?
  118.  
  119. Are you *SURE*??  There is a difference between = and == in C.  = is
  120. actually the assignment operator (ie: in a = b, a is assigned the value of
  121. b).  == is the equality test operator (ie: 1 == 1 returns TRUE (1) ).  So,
  122. word == word + 1 would alwas return false.  You should be able to do
  123. word++ (although probably the reason why you couldn't here was because
  124. word was an array of chars and the compiler didn't know what to do with
  125. word, it would've been ok if you did word[0]++, but you wouldn't have
  126. needed it anyway. 
  127.  
  128. > ps: is there a way to find out what exactly error messages mean?  I kept
  129. > getting something similar to: not an Ivalue.  It would be nice if I knew
  130. > what the hell that meant.
  131.  
  132. An lvalue is a value that can be assigned to.  For instance, if you had 3
  133. = b + 2, the compiler would complain about the 3 because you can't store
  134. another value to 3.  It probably complained about word = word + 1 because
  135. word is a "constant" that defines the beggining of the char array.  I
  136. don't really know what you get from "word" if you have a "char word[100]"
  137. array, except something really wierd, so you probably shouldn't use it.
  138.  
  139. Well, there it is!  Hope that helps!  Feel free to email me if you have
  140. other problems.
  141.  
  142. ==============================================
  143. From: Mike Dorman
  144. mikedorman@cedarnet.com
  145. 1:283/119.1012@fidonet
  146. http://www.cedarnet.com/bbs/users/mikedorman/
  147. ==============================================
  148.